﻿//----------------------------------------------------
//    版权：
//    作者：
//建立时间：2012-09-11 00:00:33
//    描述：
//----------------------------------------------------
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;

using System.Linq;
using System.Text;
using WinnerCMS.Common;

using WinnerCMS.Model;
namespace WinnerCMS.Data
{
    /// <summary>
    /// 数据访问层CMS_Digg。
    /// </summary>
    public class DiggDal
    {
        #region  添加数据
        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="model"></param>
        /// <param name="iType"></param>
        public void Save(Digg model, int iType)
        {
            using (Entities db = new Entities())
            {
                var info = db.Diggs.FirstOrDefault(x => x.ContentId == model.ContentId);
                if (info == null)
                {
                    info = db.Diggs.Create();
                    info.ContentId = model.ContentId;
                    if (iType == 1)
                    {
                        info.GoodNum = 1;
                        info.BadNum = 0;
                    }
                    else
                    {
                        info.GoodNum = 0;
                        info.BadNum = 1;
                    }
                    info.LastUserName = model.LastUserName;
                    info.LastDiggDate = DateTime.Now;
                    var diary = model.DiggDiarys.First();
                    diary.CheckInDate = DateTime.Now;
                    info.DiggDiarys.Add(diary);

                    db.Diggs.Add(info);
                }
                else
                {
                    if (iType == 1)
                    {
                        info.GoodNum = info.GoodNum + 1;
                    }
                    else
                    {
                        info.BadNum = info.BadNum + 1;
                    }
                    info.LastUserName = model.LastUserName;
                    info.LastDiggDate = DateTime.Now;

                    var diary = model.DiggDiarys.First();
                    diary.CheckInDate = DateTime.Now;
                    info.DiggDiarys.Add(diary);
                }
                db.SaveChangesAsync().Wait();
            }
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        public void Delete(int[] ids)
        {
            using (Entities db = new Entities())
            {
                var q = db.Diggs.Include(x => x.DiggDiarys).Where(x => ids.Contains(x.Id)).ToArray();
                foreach (var v in q)
                {
                    v.DiggDiarys.Clear();

                    db.Diggs.Remove(v);
                }
                db.SaveChangesAsync().Wait();
            }
        }
        #endregion

        #region 取得Digg数据
        /// <summary>
        /// 取得Digg数据
        /// </summary>
        /// <param name="contentId"></param>
        public Digg GetDigo(int contentId)
        {
            using (Entities db = new Entities())
            {
                var q = (from x in db.Diggs.AsNoTracking()
                         where x.ContentId == contentId
                         select new { x.GoodNum, x.BadNum }).ToArray().
                         Select(x => new Digg() { GoodNum = x.GoodNum, BadNum = x.BadNum });
                return q.FirstOrDefault();
            }
        }

        #endregion

        #region 绑定数据
        /// <summary>
        /// 绑定数据
        /// </summary>
        /// <param name="pp"></param>
        /// <returns></returns>
        public IList<Digg> BindList(PagingParam pp)
        {
            using (Entities db = new Entities())
            {
                db.Configuration.ProxyCreationEnabled = false;

                var query = db.Diggs.Include(x => x.Core)
                              .Where(x => x.Core != null);

                pp.RecordCount = query.Count();

                return query.OrderByDescending(x => x.Id).Take(pp.PageSize).Skip(pp.Skip).ToArray();
            }
        }

        #endregion

        #region 检查是否投票过
        /// <summary>
        /// 检查是否投票过。
        /// </summary>
        /// <param name="contentId"></param>
        /// <param name="ip"></param>
        /// <returns></returns>
        public bool CheckPosted(int contentId, string ip)
        {
            using (Entities db = new Entities())
            {
                return db.Diggs.Any(x => x.DiggDiarys.Any(y => y.IP == ip) && x.ContentId == contentId);
            }
        }

        #endregion
    }
}